﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffPDA
{
    /*
     * Class: SendOffCode
     * 
     * Use: Form displaying the disciplinary codes for a sending off
     * 
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */ 
    public partial class SendOffCode : Form
    {
        //Variables to store database connection details
        string conString;
        SqlCeConnection conn;
        
        //The current Match object
        Match theMatch;

        //Variable to indicate if home or away player is being send off
        public String homeOrAway;

        //id of the current match
        int matchId;

        //Buttons which need to be updated after the player is sent off,
        //by being coloured red and disabled later on
        public Button sendOffButton;
        public Button goalButton;
        public Button cautionButton;
        public Button subOffButton;

        //id of the player being sent off
        public int player_id;

        //Variable to store the chosen disciplinary code
        String code;
        
        public SendOffCode(Match match)
        {
            InitializeComponent();
            theMatch = match;
        }

        //This method is used to carry out the actual sending off within the database
        public void sendOffPlayer()
        {
            //Get hold of the current GPS location of the PDA device
            //for later use on the Virtual Pitch
            String gps = getGPS();

            //Connect to the database
            connectToDatabase();
            matchId = theMatch.match_id;

            //Create 'fake' GPS position for the last whistle event,
            //for use later on Virtual Pitch
            String whistleGPS = theMatch.gpsReader.adjustGPS(gps);

            //Get hold of current value of match timer
            String timeOfIncident = theMatch.tenMinutes + theMatch.minutes + ":" + theMatch.tenSeconds + theMatch.seconds;
            
            //Create and execute SQL command to record details of the sending off
            //within the database
            string command = @"INSERT INTO sendoff(sendoff_code,time,player_id,match_id,whistle_GPS,incident_GPS) VALUES(@code,@time,@id,@match,@whistleGPS,@incidentGPS);";
            using (SqlCeCommand send_off_player = new SqlCeCommand(command, conn))
            {
                send_off_player.Parameters.AddWithValue("@code", code);
                send_off_player.Parameters.AddWithValue("@time", timeOfIncident);
                send_off_player.Parameters.AddWithValue("@id", player_id);
                send_off_player.Parameters.AddWithValue("@match", matchId);
                send_off_player.Parameters.AddWithValue("@whistleGPS", whistleGPS);
                send_off_player.Parameters.AddWithValue("@incidentGPS", gps);
                send_off_player.ExecuteNonQuery();
            }
            closeDatabaseConnection();            
        }

        //This method is run if 'S1' is the chosen disciplinary code
        private void SendOffCodeS1Btn_Click(object sender, EventArgs e)
        {
            //Set the disciplinary code for use in database query
            code = "S1";

            //Edit the relevant buttons on other screens to indicate the sending
            //off, colouring them red and disabling them
            sendOffButton.BackColor = Color.Red;
            sendOffButton.Enabled = false;
            goalButton.BackColor = Color.Red;
            goalButton.Enabled = false;
            cautionButton.BackColor = Color.Red;
            cautionButton.Enabled = false;
            subOffButton.BackColor = Color.Red;
            subOffButton.Enabled = false;

            //Carry out the actual sending off within the database
            sendOffPlayer();

            //Close this form to display the main Match screen
            this.Visible = false;
        }

        //This method is run if 'S2' is the chosen disciplinary code
        private void SendOffCodeS2Btn_Click(object sender, EventArgs e)
        {
            //Set the disciplinary code for use in database query
            code = "S2";

            //Edit the relevant buttons on other screens to indicate the sending
            //off, colouring them red and disabling them
            sendOffButton.BackColor = Color.Red;
            sendOffButton.Enabled = false;
            goalButton.BackColor = Color.Red;
            goalButton.Enabled = false;
            cautionButton.BackColor = Color.Red;
            cautionButton.Enabled = false;
            subOffButton.BackColor = Color.Red;
            subOffButton.Enabled = false;

            //Carry out the actual sending off within the database
            sendOffPlayer();

            //Close this form to display the main Match screen
            this.Visible = false;
        }

        //This method is run if 'S3' is the chosen disciplinary code
        private void SendOffCodeS3Btn_Click(object sender, EventArgs e)
        {
            //Set the disciplinary code for use in database query
            code = "S3";

            //Edit the relevant buttons on other screens to indicate the sending
            //off, colouring them red and disabling them
            sendOffButton.BackColor = Color.Red;
            sendOffButton.Enabled = false;
            goalButton.BackColor = Color.Red;
            goalButton.Enabled = false;
            cautionButton.BackColor = Color.Red;
            cautionButton.Enabled = false;
            subOffButton.BackColor = Color.Red;
            subOffButton.Enabled = false;

            //Carry out the actual sending off within the database
            sendOffPlayer();

            //Close this form to display the main Match screen
            this.Visible = false;
        }

        //This method is run if 'S4' is the chosen disciplinary code
        private void SendOffCodeS4Btn_Click(object sender, EventArgs e)
        {
            //Set the disciplinary code for use in database query
            code = "S4";

            //Edit the relevant buttons on other screens to indicate the sending
            //off, colouring them red and disabling them
            sendOffButton.BackColor = Color.Red;
            sendOffButton.Enabled = false;
            goalButton.BackColor = Color.Red;
            goalButton.Enabled = false;
            cautionButton.BackColor = Color.Red;
            cautionButton.Enabled = false;
            subOffButton.BackColor = Color.Red;
            subOffButton.Enabled = false;

            //Carry out the actual sending off within the database
            sendOffPlayer();

            //Close this form to display the main Match screen
            this.Visible = false;
        }

        //This method is run if 'S5' is the chosen disciplinary code
        private void SendOffCodeS5Btn_Click(object sender, EventArgs e)
        {
            //Set the disciplinary code for use in database query
            code = "S5";

            //Edit the relevant buttons on other screens to indicate the sending
            //off, colouring them red and disabling them
            sendOffButton.BackColor = Color.Red;
            sendOffButton.Enabled = false;
            goalButton.BackColor = Color.Red;
            goalButton.Enabled = false;
            cautionButton.BackColor = Color.Red;
            cautionButton.Enabled = false;
            subOffButton.BackColor = Color.Red;
            subOffButton.Enabled = false;

            //Carry out the actual sending off within the database
            sendOffPlayer();

            //Close this form to display the main Match screen
            this.Visible = false;
        }

        //This method is run if 'S6' is the chosen disciplinary code
        private void SendOffCodeS6Btn_Click(object sender, EventArgs e)
        {
            //Set the disciplinary code for use in database query
            code = "S6";

            //Edit the relevant buttons on other screens to indicate the sending
            //off, colouring them red and disabling them
            sendOffButton.BackColor = Color.Red;
            sendOffButton.Enabled = false;
            goalButton.BackColor = Color.Red;
            goalButton.Enabled = false;
            cautionButton.BackColor = Color.Red;
            cautionButton.Enabled = false;
            subOffButton.BackColor = Color.Red;
            subOffButton.Enabled = false;

            //Carry out the actual sending off within the database
            sendOffPlayer();

            //Close this form to display the main Match screen
            this.Visible = false;
        }

        //This method is run if 'S7' is the chosen disciplinary code
        private void SendOffCodeS7Btn_Click(object sender, EventArgs e)
        {
            //Set the disciplinary code for use in database query
            code = "S7";

            //Edit the relevant buttons on other screens to indicate the sending
            //off, colouring them red and disabling them
            sendOffButton.BackColor = Color.Red;
            sendOffButton.Enabled = false;
            goalButton.BackColor = Color.Red;
            goalButton.Enabled = false;
            cautionButton.BackColor = Color.Red;
            cautionButton.Enabled = false;
            subOffButton.BackColor = Color.Red;
            subOffButton.Enabled = false;

            //Carry out the actual sending off within the database
            sendOffPlayer();

            //Close this form to display the main Match screen
            this.Visible = false;
        }

        //This method is used to obtain the current GPS location of the device
        //for use later on the Virtual Pitch
        private String getGPS()
        {
            //Get hold of the current GPS location of the device
            String gps_location = theMatch.gpsReader.readGPS();
            return gps_location;
        }

        //Method to connect to the database
        private void connectToDatabase()
        {
            // Retrieve the connection string
            conString = @"Data Source=\Program Files\RefOffPDA\RefOffPDA_DB.sdf";

            // Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //Method to disconnect from the database
        private void closeDatabaseConnection()
        {
            conn.Close();
        }

        //This method is run when the 'Cancel' button is clicked
        private void CancelButton_Click(object sender, EventArgs e)
        {
            //Hide this form and display the main Match screen
            this.Visible = false;
        }
    }
}